import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_jwt_extended import JWTManager

# 初始化数据库类db
db = SQLAlchemy()
migrate = Migrate()


# 和app绑定，创建数据库
def init_databases(app: Flask):
    db.init_app(app)
    migrate.init_app(app, db)
    if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
        with app.app_context():
            try:
                db.engine.connect()
            except Exception as e:
                exit(f"数据库连接失败: {e}")